package ar.com.android.alco.forms;


import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import ar.com.android.alco.adapters.CalendarioGridCellAdapter;
import ar.com.android.alco.dbHelpers.SQLALCOHelper;
import ar.com.android.alco.providers.RegistroComidasProvider;

public class RegistroCalendarioActivity extends Activity {
	private static final String TAG = "RegistroCalendarioActivity";
	
	private int[] monthIds = {R.string.january,R.string.february,R.string.march,R.string.april,R.string.may,R.string.june,R.string.july,R.string.august,R.string.september,R.string.october,R.string.november,R.string.december};
	
	private ImageView prevMonth;
	private ImageView nextMonth;
	private TextView txtMes;
	private TextView txtAnno;
	private TextView tvFecha;
	private TextView tvCreditosConsumidosVal;
	private GridView calendarView;
	
	private CalendarioGridCellAdapter adapter;
	private Calendar _calendar;
	private int month, year;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registro_calendario);
        
		_calendar = Calendar.getInstance(Locale.getDefault());
		month = _calendar.get(Calendar.MONTH);
		year = _calendar.get(Calendar.YEAR);
		
		txtMes = (TextView) findViewById(R.id.txtMes);
		txtAnno = (TextView) findViewById(R.id.txtAnno);
		tvFecha = (TextView) findViewById(R.id.tvValFecha);
		tvCreditosConsumidosVal = (TextView) findViewById(R.id.tvCreditosConsumidosVal);
		calendarView = (GridView) this.findViewById(R.id.calendar);
		
		tvFecha.setText("");
		
		txtMes.setText(monthIds[month]);
		txtAnno.setText(String.valueOf(year));

		prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
		prevMonth.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (month <= 0) {
					month = 11;
					year--;
				}
				else
					month--;
				
				adapter = new CalendarioGridCellAdapter(RegistroCalendarioActivity.this, month, year);
				_calendar.set(year, month, _calendar.get(Calendar.DAY_OF_MONTH));
				txtMes.setText(monthIds[month]);
				txtAnno.setText(String.valueOf(year));
				adapter.notifyDataSetChanged();
				calendarView.setAdapter(adapter);
			}
		});

		nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
		nextMonth.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if (month >= 11) {
					month = 0;
					year++;
				}
				else
					month++;
				
				adapter = new CalendarioGridCellAdapter(RegistroCalendarioActivity.this, month, year);
				_calendar.set(year, month, _calendar.get(Calendar.DAY_OF_MONTH));
				txtMes.setText(monthIds[month]);
				txtAnno.setText(String.valueOf(year));
				adapter.notifyDataSetChanged();
				calendarView.setAdapter(adapter);
			}
		});

		calendarView = (GridView) this.findViewById(R.id.calendar);

		// Initialised
		adapter = new CalendarioGridCellAdapter(RegistroCalendarioActivity.this, month, year);
		adapter.notifyDataSetChanged();
		calendarView.setAdapter(adapter);
		
		tvFecha.addTextChangedListener(new TextWatcher() {
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				SQLALCOHelper sqlHelper = SQLALCOHelper.getHelper(RegistroCalendarioActivity.this);
				sqlHelper.dbOpen();
				
				try
				{
			    	RegistroComidasProvider registroComidasProvider = new RegistroComidasProvider(RegistroCalendarioActivity.this);
			    	Double dblCreditosConsumidos = (double) Math.round(registroComidasProvider.getSumaCreditosDia(tvFecha.getText().toString())*100.0)/100.0;
					tvCreditosConsumidosVal.setText(dblCreditosConsumidos.toString());
				}
				catch (Exception e) {
					Log.d(TAG, e.getMessage());
				}
				finally{
					sqlHelper.dbClose();
				}
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void afterTextChanged(Editable s) {}
		});
    }
    
    public void onClickCargarRegistro(View v) throws Exception
    {
    	if(tvFecha.getText() == "") {
			LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.drawable.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

			TextView text = (TextView) layout.findViewById(R.id.text);
			text.setText(getText(R.string.msgFecha));

			Toast toast = new Toast(getApplicationContext());
			toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
			toast.setDuration(Toast.LENGTH_LONG);
			toast.setView(layout);
			toast.show();	
    	}
    	else {
			Intent intentCargarRegistro = new Intent(this, RegistroIngresoActivity.class);
			intentCargarRegistro.putExtra("fechaRegistro", tvFecha.getText().toString());
			startActivityForResult(intentCargarRegistro, 1);
    	}
    }
    
    public void onClickConsultaRegistro(View v) throws Exception
    {
    	if(tvFecha.getText() == "") {
			LayoutInflater inflater = getLayoutInflater();
			View layout = inflater.inflate(R.drawable.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

			TextView text = (TextView) layout.findViewById(R.id.text);
			text.setText(getText(R.string.msgFecha));

			Toast toast = new Toast(getApplicationContext());
			toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
			toast.setDuration(Toast.LENGTH_LONG);
			toast.setView(layout);
			toast.show();	
    	}
    	else {
			Intent intentConsultaRegistro = new Intent(this, RegistroConsultaActivity.class);
			intentConsultaRegistro.putExtra("fechaRegistro", tvFecha.getText().toString());
			startActivityForResult(intentConsultaRegistro, 1);
    	}
    }
}